package com.starsea.book.ui.newpage.weidget.banner.transformer

import android.view.View
import androidx.core.view.ViewCompat

/**
 * 描述:
 */
class ZoomPageTransformer : BannerPageTransformer {
  private var mMinScale = 0.85f
  private var mMinAlpha = 0.65f

  constructor()
  constructor(minAlpha: Float, minScale: Float) {
    setMinAlpha(minAlpha)
    setMinScale(minScale)
  }

  override fun handleInvisiblePage(view: View, position: Float) {
    ViewCompat.setAlpha(view, 0f)
  }

  override fun handleLeftPage(view: View, position: Float) {
    val scale = Math.max(mMinScale, 1 + position)
    val vertMargin = view.height * (1 - scale) / 2
    val horzMargin = view.width * (1 - scale) / 2
    ViewCompat.setTranslationX(view, horzMargin - vertMargin / 2)
    ViewCompat.setScaleX(view, scale)
    ViewCompat.setScaleY(view, scale)
    ViewCompat.setAlpha(view, mMinAlpha + (scale - mMinScale) / (1 - mMinScale) * (1 - mMinAlpha))
  }

  override fun handleRightPage(view: View, position: Float) {
    val scale = Math.max(mMinScale, 1 - position)
    val vertMargin = view.height * (1 - scale) / 2
    val horzMargin = view.width * (1 - scale) / 2
    ViewCompat.setTranslationX(view, -horzMargin + vertMargin / 2)
    ViewCompat.setScaleX(view, scale)
    ViewCompat.setScaleY(view, scale)
    ViewCompat.setAlpha(view, mMinAlpha + (scale - mMinScale) / (1 - mMinScale) * (1 - mMinAlpha))
  }

  fun setMinAlpha(minAlpha: Float) {
    if (minAlpha >= 0.6f && minAlpha <= 1.0f) {
      mMinAlpha = minAlpha
    }
  }

  fun setMinScale(minScale: Float) {
    if (minScale >= 0.6f && minScale <= 1.0f) {
      mMinScale = minScale
    }
  }
}